package com.oa.admin.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.DepartmentApplyListDTO;
import com.oa.admin.dto.DepartmentalBudgetListDTO;
import com.oa.admin.entity.DepartmentalBudget;
import com.oa.admin.service.IDepartmentApplyService;
import com.oa.admin.service.IDepartmentalBudgetService;
import com.oa.admin.vo.DepartmentalBudgetDetailsVO;
import com.oa.core.utils.FastJsonSerializerUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.HashMap;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/departmentalBudget")
@Api(description = "部门经费表模块")
public class DepartmentalBudgetController extends BaseController {

    private final IDepartmentalBudgetService departmentalBudgetService;

    @Autowired
    private IDepartmentApplyService departmentApplyService;

    @Autowired
    public DepartmentalBudgetController(IDepartmentalBudgetService departmentalBudgetService) {
        this.departmentalBudgetService = departmentalBudgetService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询（不传page则查询所有）")
    public Resp findList(DepartmentalBudgetListDTO departmentalBudgetListDTO) {
        if(departmentalBudgetListDTO.getDepartmental() == null){
            departmentalBudgetListDTO.setDepartmental("");
        }
        return new Resp().success(departmentalBudgetService.findDepartmentalBudgetList(departmentalBudgetListDTO.getPageNum(), departmentalBudgetListDTO.getPageSize(), departmentalBudgetListDTO.getDepartmental()));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<DepartmentalBudgetDetailsVO> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(departmentalBudgetService.findDepartmentalBudgetById(id));
    }


    /**
     * 新增
     *
     * @param departmentalBudget DepartmentalBudget
     * @return Resp
     */
    @PostMapping("/add")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "部门经费表对象") @RequestBody(required = false) DepartmentalBudget departmentalBudget) {
        if (departmentalBudgetService.insert(departmentalBudget)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id修改
     *
     * @param departmentalBudget DepartmentalBudget
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "部门经费表对象") @RequestBody(required = false) DepartmentalBudget departmentalBudget) {
        if (departmentalBudgetService.updateById(departmentalBudget)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        DepartmentalBudget departmentalBudget = departmentalBudgetService.selectById(id);
        if (departmentalBudget != null && departmentalBudget.getIsDeleted() == 2) {
            departmentalBudget.setIsDeleted(1);
            if (departmentalBudgetService.updateById(departmentalBudget)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
